package com.longcai.piaojia.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.longcai.piaojia.BaseApplication;
import com.longcai.piaojia.R;
import com.longcai.piaojia.adapter.ReceiveAddressListAdapter;
import com.longcai.piaojia.conn.DeleteAddressGet;
import com.longcai.piaojia.conn.SetDefaultAddressGet;
import com.longcai.piaojia.conn.UserAddressShowGet;
import com.longcai.piaojia.dialog.WarnningDialog;
import com.longcai.piaojia.util.MyToast;
import com.longcai.piaojia.widget.NoScrollListView;
import com.zcx.helper.app.AppCallBack;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/8/2.
 */
public class MyReceiveAddressActivity extends BaseActivity implements View.OnClickListener, ReceiveAddressListAdapter.SetDefaultAddressListener {

    @BoundView(R.id.receive_address_lv) private ListView receive_address_lv;
    @BoundView(R.id.receive_address_add_tv) private TextView receive_address_add_tv;

    private ReceiveAddressListAdapter receiveAddressListAdapter;

    private List<UserAddressShowGet.UserAddressEntity> lists = new ArrayList<>();

    private int position = -1; // 设置默认地址的那条  position

    private boolean isChoose = false;

    private int deletePos; // 删除那条的 position

    private UserAddressShowGet userAddressShowGet = new UserAddressShowGet(new AsyCallBack<UserAddressShowGet.UserAddressListEntity>(){

        @Override
        public void onSuccess(String toast, int type, UserAddressShowGet.UserAddressListEntity userAddressListEntity) throws Exception {

            lists.addAll(userAddressListEntity.userAddressEntityList);
            receiveAddressListAdapter.notifyDataSetChanged();

        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            super.onFail(toast, type);

            Toast.makeText(MyReceiveAddressActivity.this, toast, Toast.LENGTH_LONG).show();

        }
    });

    private SetDefaultAddressGet setDefaultAddressGet = new SetDefaultAddressGet(new AsyCallBack<String>(){

        @Override
        public void onSuccess(String toast, int type, String s) throws Exception {

            if(position!=-1){

                for(int i=0;i<lists.size();i++){

                    if(i==position){

                        lists.get(i).status = "1";

                    }else{

                        lists.get(i).status = "0";

                    }

                }

                receiveAddressListAdapter.notifyDataSetChanged();

                try {
                    DispatchingTypeActivity.RefreshDefaultAddress refreshDefaultAddress = (DispatchingTypeActivity.RefreshDefaultAddress) getAppCallBack(DispatchingTypeActivity.class);
                    refreshDefaultAddress.RefreshAddress(1);
                } catch (Exception e) {
                    e.printStackTrace();
                }



            }

            Toast.makeText(MyReceiveAddressActivity.this, toast, Toast.LENGTH_LONG).show();

        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            super.onFail(toast, type);

            Toast.makeText(MyReceiveAddressActivity.this, toast, Toast.LENGTH_LONG).show();

        }

    });


    private DeleteAddressGet deleteAddressGet = new DeleteAddressGet(new AsyCallBack<String>(){

        @Override
        public void onSuccess(String toast, int type, String s) throws Exception {

            lists.remove(deletePos);

            receiveAddressListAdapter.notifyDataSetChanged();

            MyToast.show(context, toast, 2000);

            if(lists.size()==0 || lists.get(deletePos).status.equals("1")){

                try {
                    DispatchingTypeActivity.RefreshDefaultAddress refreshDefaultAddress = (DispatchingTypeActivity.RefreshDefaultAddress) getAppCallBack(DispatchingTypeActivity.class);
                    refreshDefaultAddress.RefreshAddress(5);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

        }

        @Override
        public void onFail(String toast, int type) throws Exception {


            MyToast.show(context, toast, 2000);
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_receive_address);

        isChoose = getIntent().getBooleanExtra("isChoose", false);

        setBackTrue();

        setTitleName(getResources().getString(R.string.receive_address_title_text));

        receive_address_lv.setDivider(null);

        receive_address_lv.setAdapter(receiveAddressListAdapter = new ReceiveAddressListAdapter(context, lists, this));

        receive_address_add_tv.setOnClickListener(this);

        receive_address_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                if(isChoose){

//                    Toast.makeText(MyReceiveAddressActivity.this, "这里写回调到选择收货方式里面的选择地址里面", Toast.LENGTH_LONG).show();

                    Intent intent = new Intent(MyReceiveAddressActivity.this, DispatchingTypeActivity.class);

                    Bundle bundle = new Bundle();

                    bundle.putString("name",lists.get(position).name);
                    bundle.putString("address",lists.get(position).city + getString(R.string.space) + lists.get(position).address);
                    bundle.putString("phone",lists.get(position).phone);

                    intent.putExtras(bundle);

                    MyReceiveAddressActivity.this.setResult(5, intent);

                    finish();


                }

            }
        });

        setAppCallBack(new refreshAddressCallback());

        new refreshAddressCallback().refreshAddress();

    }

    @Override
    public void onClick(View view) {

        switch (view.getId()){

            case R.id.receive_address_add_tv:

                Intent intent = new Intent(MyReceiveAddressActivity.this, MyReceiveAddressAddActivity.class);
                intent.putExtra("isChoose", false);
                startActivity(intent);

                break;

        }
    }


    @Override
    public void setDefault(final int pos) {

        new WarnningDialog(context, getResources().getString(R.string.item_receive_address_set_default_text)) {
            @Override
            protected void onConfirm() {

                position = pos;

                setDefaultAddressGet.user_id = BaseApplication.BasePreferences.readUserId();
                setDefaultAddressGet.id = lists.get(pos).id;
                setDefaultAddressGet.execute(MyReceiveAddressActivity.this);

            }
        }.show();

    }

    @Override
    public void setUpdate(int pos) {

        Intent intent = new Intent(MyReceiveAddressActivity.this, MyReceiveAddressEditActivity.class);

        Bundle bundle = new Bundle();

        bundle.putSerializable("UserAddressEntity", lists.get(pos));

        bundle.putBoolean("isChoose", isChoose);

        intent.putExtras(bundle);

        startActivity(intent);

    }

    @Override
    public void setDelete(final int pos) {

//        Toast.makeText(MyReceiveAddressActivity.this, "删除接口没给", Toast.LENGTH_LONG).show();

        new WarnningDialog(MyReceiveAddressActivity.this, getString(R.string.mycollection_delete_text)) {
            @Override
            protected void onConfirm() {


                deletePos = pos;

                deleteAddressGet.id = lists.get(pos).id;
                deleteAddressGet.user_id = BaseApplication.BasePreferences.readUserId();

                deleteAddressGet.execute(MyReceiveAddressActivity.this);


            }
        }.show();

    }

    public class refreshAddressCallback implements AppCallBack{

        public void refreshAddress() {

            lists.clear();

            userAddressShowGet.user_id = BaseApplication.BasePreferences.readUserId();
            userAddressShowGet.execute(MyReceiveAddressActivity.this);

        }
    }



}
